﻿@model Bricks.Business.Models.Account.OrgInfo

@{
    ViewBag.Title = "机构/用户管理";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

@*实现对EasyUI的DataGird控件操作的JS代码*@
<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        initTreeTable();

        //var f = $('#embed');
        //f.load = "/Main/Portal";

        //$('#embed').load("/Account/Index?MenuID=B4965782-329A-4A33-82EC-A8353E7054EB");
        //$.parser.parse('#embed');


        $('#embed').panel({
            //href: '/Account/Index?MenuID=B4965782-329A-4A33-82EC-A8353E7054EB',
            href: '/Account/Index?MenuCode=Account'
        });
    });
    //实现对DataGird控件的绑定操作

    function initTreeTable(queryData) {
        $('#myTreeGrid').treegrid({   //定位到Table标签，Table标签的ID是myTreeGrid
            //   OrgInfo是控制器，GetData是控制器中获取用户数据的一个Action
            url: '/OrgInfo/GetDataList',   //指向后台的Action来获取当前用户的信息的Json格式的数据
            //title: '机构管理',  //标识
            //iconCls: 'icon-user',
            //width: 'auto',
            //height: 500,
            nowrap: true,
            autoRowHeight: false,
            singleSelect: true,
            striped: true,
            collapsible: true,
            pagination: false,
            rownumbers: true,
            //sortName: 'ID',    //根据某个字段给easyUI排序
            //sortOrder: 'asc',
            remoteSort: false,
            idField: 'ID',
            treeField: 'OrgName',
            parentField: 'PID',
            queryParams: queryData,  //异步查询的参数
            toolbar: '#myTreeToolBar',
            columns: [[
                @*{
                        title: '@Html.LabelFor(model => model.ID)', field: 'ID', width: 265, sortable: true,
                        styler: function (value, row, index) {
                            if (index < 6) {
                                return 'background-color:#ffee00;color:red;';
                            }
                        }
                    },  //主键*@
                { title: '@Html.LabelFor(model => model.OrgName)', field: 'OrgName', width: 165, sortable: false },
                @*{ title: '@Html.LabelFor(model => model.OrgCode)', field: 'OrgCode', width: 100, sortable: true },
                { title: '@Html.LabelFor(model => model.Email)', field: 'Email', width: 180, sortable: true },
                { title: '@Html.LabelFor(model => model.Contactphone)', field: 'Contactphone', width: 300, sortable: true },
                //{ title: '@Html.LabelFor(model => model.PID)', field: 'PID', width: 80, sortable: true },
                { title: '@Html.LabelFor(model => model.OrgTypeNum)', field: 'OrgTypeString', width: 70, sortable: true },
                //{ title: '@Html.LabelFor(model => model.OrderNo)', field: 'OrderNo', width: 40, sortable: true },
                { title: '@Html.LabelFor(model => model.Description)', field: 'Description', width: 200, sortable: true },*@
                {
                    title: '@Html.LabelFor(model => model.IsDeleted)', field: 'IsDeleted', width: 40, sortable: false, align: 'center',
                    formatter: function (value, row, index) {
                        return $.GetYesOrNo(value);  //是否禁用图标（是/否）
                    }
                }
            ]],
            //点击事件的处理：过滤部门下的用户
            onClickRow: function (index, row) {
                $('#myDataGrid').datagrid('unselectAll');
                //$('#myDataGrid').datagrid('load', { orgID: row.ID });

                //以下方法可行
                var selectedRow = $("#myTreeGrid").treegrid("getSelected");
                if (selectedRow) {
                    var obj = document.getElementById("isContainsChildrenOrg");
                    var isContainsChildrenOrg = obj.checked;  //取得值（是否显示下级机构）
                    $('#myDataGrid').datagrid('load', { orgID: selectedRow.ID, isContainsChildrenOrg: isContainsChildrenOrg });
                }
                //$.EasyuiSlide("点击事件测试");
            }
        });
    }
    var OrgInfo = {
        //新增
        Add: function () {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                OrgInfo.Mutual("新增", "/OrgInfo/Edit?PID=" + row.ID);
            }
            else { OrgInfo.Mutual("新增", "/OrgInfo/Edit"); }
        },

        //修改
        Modify: function () {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                OrgInfo.Mutual("修改", "/OrgInfo/Edit?ID=" + row.ID);
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //实体操作
        Mutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 320,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-model",
                        url: "/OrgInfo/Edit",
                        success: function (result) { OrgInfo.Reload(); }
                    });
                },
                onLoad: function () { OrgInfo.FormInit(); }
            });
        },

        //弹窗初始化
        FormInit: function () {
            //定义父级的机构树
            $("#PID").combotree({
                width: 155, url: "/OrgInfo/GetOrgComboTree"
                //onLoadSuccess: function (node, data) {
                //    var nodeid = $("#ID").val();
                //    if (nodeid) {
                //        var etree = $("#PID").combotree("tree");
                //        etree.tree("remove", etree.tree('find', nodeid).target);
                //    }
                //}
            });
            //定义机构类型枚举下拉（暂时屏蔽机构类型）
            //$("#OrgTypeNum").combobox({
            //    editable: false, width: 155, panelHeight: "auto",
            //    data: [
            //        { value: "0", text: "未指定" },
            //        { value: "1", text: "公司" },
            //        { value: "2", text: "部门" },
            //        { value: "3", text: "岗位" }
            //    ]
            //});
        },

        //删除
        Delete: function () {
            var arr = $('#myTreeGrid').datagrid('getSelections');

            if (arr.length == 1) {
                $.messager.confirm('操作提示', "确认删除[ " + arr[0].OrgName + " ]吗？", function (r) {
                    if (r) {
                        $.post("/OrgInfo/Delete?ID=" + arr[0].ID, { query: arr.join(",") }, function (result) {
                            if (result.ResultType == 0) {  //0表示成功
                                //移除删除的数据
                                //$("#myTreeGrid").datagrid("reload");
                                //$("#myTreeGrid").datagrid("clearSelections");
                                //$.messager.alert('操作提示', '删除成功!', 'info');
                                $.EasyuiSlide(result.Message);
                                OrgInfo.Reload();
                            }
                            else {
                                $.EasyuiMsg(result.Message);
                            }
                        });
                    }
                });

            } else {
                $.messager.alert('操作提示', '请选择一条数据!', 'warning');
            }
            return false;
        },

        //刷新重载
        Reload: function () {
            $("#myTreeGrid").treegrid("reload");  //注意此处是树表
        },

        //向上
        Up: function () {
            OrgInfo.Order("Up");
        },

        //向下
        Down: function () {
            OrgInfo.Order("Down");
        },

        //向上或向下方法
        Order: function (order) {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                //id：识别当前记录；pid：识别涉及调整的记录集合；order：向上或向下
                $.post("/OrgInfo/SetOrder", { id: row.ID, pid: row.PID, order: order }, function (result) {
                    if (result.ResultType == 0) {
                        $.EasyuiSlide(result.Message);  //成功则右下角提示
                        OrgInfo.Reload();
                        //$("#myTreeGrid").treegrid("selectRecord", row.ID);  //重新选中对应ID的记录（此处不生效，原因不明，后续探究）
                    }
                    else {
                        $.EasyuiMsg(result.Message);  //失败则弹出框提示
                    }
                });
            }
            else { $.EasyuiSlide(nullrowmsg); }
        },

        //机构授权
        Purview: function () {
            var arr = $('#myTreeGrid').datagrid('getSelections');
            if (arr.length == 1) {
                OrgInfo.OrgPurviewMutual("机构授权", "/Purview/OrgPurview?OrgID=" + arr[0].ID);
            } else {
                //$.messager.alert('操作提示', '请选择一条数据!', 'warning');
                $.EasyuiMsg("请选择一条数据");
            }
        },

        //机构授权操作
        OrgPurviewMutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 650, maximizable: true,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-orgPurview",
                        url: "/Purview/OrgPurviewPost",
                        success: function (result) {
                            //OrgInfo.Reload();
                        }
                    });
                },
                onLoad: function () {
                    //OrgInfo.FormInit();
                }
            });
        }
    }
</script>

<div class="easyui-layout" data-options="fit:true">
    <!--左边为机构管理列表-->
    <div region="west" data-options="border:false" style="width: 255px;">
        <div class="easyui-panel" @Html.Raw(ViewBag.TreePanelInfo)>
            <table id="myTreeGrid" fit="true"></table>
            <!--数据列表（根据JS构造数据）-->
            <div id="myTreeToolBar">@Html.Raw(ViewBag.TreeToolBarInfo)</div>
            <!--工具栏（从数据库取得菜单菜单的名称和图标等）-->
        </div>
    </div>
    <!--右边为用户管理列表，根据选择的机构进行切换-->
    @*<div id="embed" region="center" style="border-left: 0px;">@Html.Raw(ViewBag.EmbedInfo)</div>*@
    <div region="center" style="border: 0px;height:500px;width:100%;">
        <div id="embed" class="easyui-panel" fit="true"></div>
    </div>
</div>
